home *** CD-ROM | disk | FTP | other *** search
- <SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
- <!--#INCLUDE FILE="MSGlobal.inc"-->
- ////////////////////////////////////////////////////////////////////
- // //
- // Fusion ASP Components //
- // Custom JScript Object: MSDBUpdate //
- // //
- ///////////////////////////////////////////////////////////////////*
- /*
- Object: MSDBUpdate
-
- Version: 1.0 9/23/97
-
- Written By: Application Methods, Inc.
- 6300 Southcenter Blvd.
- Seattle, WA 98188
- (206) 244-2400
- http://www.appmethods.com
-
- Description: The MSDBUpdate button object is responsible for modifying
- the specified record of a table. The button on the page is a
- standard submit button which points to a special form handler.
- The object passes its properties to the form handler by creating
- hidden fields for each important parameter. This is done by the
- render method.
-
- Note: Generation of a number of hidden fields is necessary to pass information
- on to other components. All hidden fields created by this object are prefixed
- with 'amaspHidden_' or 'amaspComponent_'. Developers should never create
- any form element with these prefixes or unexpected results will occur.
-
- Properties:
- name - Name of Update object
- action - Action to be taken on the table
- queryComponent - name of query component from which to extract the Primary key field values
- tableName - Name of the table to edit
-
- successURL - Relative URL of success page
- errorURL - Relative URL of error page
-
- primaryKeyFieldCount - Count of primary key fields
- primaryKeyFieldNames - Names of primary key fields
- primaryKeyFieldValues - Values of primary key fields (optional, may constructed by object)
- primaryKeyFieldTypes - Types of the primary key fields
-
- fieldCount - Count of update fields (optional, may be constructed by form handler)
- fieldNames - Names of update fields (optional, may be constructed by form handler)
- fieldValues - Values of update fields (optional, may be constructed by form handler)
- fieldTypes - Types of the primary key fields (optional, may be constructed by form handler)
-
- Methods:
- render - renders necessary hidden fields
- emitProperties - writes all object properties
- getValues - retrieves primary key field values
- buildAddSQL - builds adds SQL
- buildDeleteSQL - builds delete SQL
- buildModifySQL - builds modify SQL
- execute - gets database, builds & executes SQL
- buildWhereSQL - builds where portion of SQL
- setDatabaseProperties - sets properties for database and ODBC database type
-
- Usage: The following example shows a value which may be edited and updated
- by the user.
-
- <HTML>
- <HEAD>
-
- <!SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
-
- // Create and connect the database object
- MSDBConnection1 = new MSDBConnection("test", "ODBC","MS Access", "Northwind","admin","",false);
- MSDBConnection1.connect();
-
- // Create the query object
- MSDBQuery1 = new MSDBQuery("MSDBQuery1", "MSDBConnection1", false, "*", "Customers", "customerID > 7", "lastname");
-
- // Create the dynafield object
- MSDBDynaField1 = new MSDBDynaField( "MSDBDynaField1",
- "true",
- "MSDBQuery1",
- "",
- "ShipCity",
- "string",
- "false",
- 10,
- 10,
- 1,
- "Arial",
- "+0",
- "black",
- "false",
- "false",
- "false");
- // Create update object
-
- MSDBUpdate1primaryKeyFieldNames = new Array(1);
- MSDBUpdate1primaryKeyFieldDataTypes = new Array(1);
-
- MSDBUpdate1primaryKeyFieldNames[0] = "CustomerID"
- MSDBUpdate1primaryKeyFieldDataTypes[0] = "string"
-
- MSDBUpdate1 = new MSDBUpdate( "MSDBUpdate1", "modify", "MSDBQuery1", "Customers",
- "../html/Success.html", "../html/Error.html",
- 1, MSDBUpdate1primaryKeyFieldNames, null, MSDBUpdate1primaryKeyFieldDataTypes,
- null, null, null, null) {
-
- <!/SCRIPT>
-
- <META NAME="Generator" CONTENT="NetObjects Fusion 2.0 for Windows">
-
- </HEAD>
- <body>
-
- write("<FORM METHOD=POST>");
- <%
- MSDBConnection1.render()
- MSDBDynaField1.render()
- MSDBUpdate1.render()
- %>
- write("</FORM>");
-
- </BODY>
- </HTML>
-
-
- =====================================================================*/
-
- //
- // MSDBUpdate Object Constructor
- //
- function MSDBUpdate (name, action, queryComponent, tableName,
- successURL, errorURL,
- primaryKeyFieldCount, primaryKeyFieldNames, primaryKeyFieldValues, primaryKeyFieldDataTypes,
- fieldCount, fieldNames, fieldValues, fieldDataTypes ) {
-
- // Properties
- this.name = (name != null) ? name : "";
- this.dataSource = "MSDBConnection1";
- this.action = (action != null) ? action : "";
- this.queryComponent = (queryComponent != null) ? queryComponent : "";
- this.tableName = (tableName != null) ? tableName : "";
- this.databaseType = "";
- this.ODBCDatabaseType = "";
- this.primaryKeyFieldCount = (primaryKeyFieldCount+"" != "null") ? primaryKeyFieldCount: 0;
-
- if (primaryKeyFieldNames != null) {
- this.primaryKeyFieldNames = primaryKeyFieldNames;
- }
- else {
- this.primaryKeyFieldNames = new Array();
- for (var i = 0; i < parseInt(primaryKeyFieldCount,10) - 1; i++) this.primaryKeyFieldNames[i]="";
- }
-
- if (primaryKeyFieldValues != null) {
- this.primaryKeyFieldValues = primaryKeyFieldValues;
- }
- else {
- this.primaryKeyFieldValues = new Array();
- for (var i = 0; i < parseInt(primaryKeyFieldCount) - 1; i++) this.primaryKeyFieldValues[i]="";
- }
-
- if (primaryKeyFieldDataTypes != null) {
- this.primaryKeyFieldDataTypes = primaryKeyFieldDataTypes;
- }
- else {
- this.primaryKeyFieldDataTypes = new Array();
- for (var i = 0; i < parseInt(primaryKeyFieldCount) - 1; i++) this.primaryKeyFieldDataTypes[i]="";
- }
-
- this.successURL = (successURL != null) ? successURL : "";
- this.errorURL = (errorURL != null) ? errorURL : "";
-
- this.fieldCount = ((fieldCount+"" != "null") && (fieldCount+"" != "undefined") && (fieldCount != null)) ? fieldCount : 0;
-
- if (fieldNames != null) {
- this.fieldNames = fieldNames;
- }
- else {
- this.fieldNames = new Array();
- for (var i = 0; i < this.fieldCount - 1; i++) this.fieldNames[i]="";
- }
-
- if (fieldValues != null) {
- this.fieldValues = fieldValues;
- }
- else {
- this.fieldValues = new Array();
- for (var i = 0; i < this.fieldCount - 1; i++) this.fieldValues[i]="";
- }
-
- if (fieldDataTypes != null) {
- this.fieldDataTypes = fieldDataTypes;
- }
- else {
- this.fieldDataTypes = new Array();
- for (var i = 0; i < this.fieldCount - 1; i++) this.fieldDataTypes[i]="";
- }
-
- // Methods
- this.render = utRender;
- this.emitProperties = utEmitProperties;
- this.getValues = utGetValues;
- this.buildAddSQL = utBuildAddSQL;
- this.buildDeleteSQL = utBuildDeleteSQL;
- this.buildModifySQL = utBuildModifySQL;
- this.execute = utExecute;
- this.buildWhereSQL = utBuildWhereSQL;
- this.setDatabaseTypes = utSetDatabaseTypes;
-
- if (this.action != "add") {
- // Increment global cursor usage counter since this component will try to use the cursor
- incCursorCallCount();
- }
-
- } // END MSDBUpdate constructor
-
-
- //
- // This method will first check primary key fields for existance then fill the
- // Primary Key Field Value array with the corresponding values for each field.
- // Errors are printed for null cursor, failure to advance to first record as
- // necessary and non-existance of field in query.
- //
- function utGetValues() {
-
- //debug("Getting field values")
-
- var fieldError = true;
-
- var fcnt;
- var ccnt;
-
- //debug("Getting cursor from query '"+ this.queryComponent + "'");
- myCursor = eval(this.queryComponent + ".getCurrentCursor()");
- if (myCursor != void(0)) {
- if (myCursor != null) {
-
- if (eval(this.queryComponent + ".initializeCursor()")) {
-
- //Check primary key field names for existance
- //debug("searching fields");
- for (fcnt = 0; fcnt < parseInt(this.primaryKeyFieldCount,10); fcnt++) {
- fieldError=true;
- for (ccnt = 0; ccnt < myCursor.Fields.Count; ccnt++) {
- if (myCursor.Fields(ccnt).Name.toUpperCase() == this.primaryKeyFieldNames[fcnt].toUpperCase()) {
- fieldError=false;
- } // end if
- } // end if fields for
- if (fieldError) {
- debug("field error found");
- write("\n<P>***Warning: Primary key field name: '" + this.primaryKeyFieldNames[fcnt] + "' not found in query");
- }
- }
-
- // get values for each primary key field
- for (var i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
- this.primaryKeyFieldValues[i]=formatDataValue(myCursor.Fields(this.primaryKeyFieldNames[i]).Value, this.primaryKeyFieldDataTypes[i]);
- }
- }
- else {
- // write("\n<P>***Warning: No records found when attempting to get primary key values.");
- }
- }
- else {
- write("\n<P>***Warning: Unable to obtain a valid cursor when attempting to get primary key values.");
- }
- }
- else {
- write("\n<P>***Warning: Unable to obtain a valid cursor.");
- }
-
-
- } // END utCheckFieldErrors
-
-
- //
- // This method renders all hidden fields from object properties
- //
- function utRender() {
-
- //debug("Rendering hidden fields")
-
- if (this.action != "add")
- this.getValues();
-
- //this.emitProperties();
- // Temporary - Change later
- var pre = "amaspHidden_"+this.name+"_";
-
- // Emit other properties
- write("\n<INPUT NAME = \""+pre+"tableName\" VALUE = \"" + this.tableName + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \""+pre+"primaryKeyFieldCount\" VALUE = \"" + this.primaryKeyFieldCount + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \""+pre+"successURL\" VALUE = \"" + this.successURL + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \""+pre+"errorURL\" VALUE = \"" + this.errorURL + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \""+pre+"action\" VALUE = \"" + this.action + "\" TYPE = \"HIDDEN\">");
-
- // Emit primary key fields
- if (this.action != "add") {
- for (var i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
- write("\n<INPUT NAME = \"" + pre + "primaryKeyFieldNames"+ i + "\" VALUE = \"" + this.primaryKeyFieldNames[i] + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \"" + pre + "primaryKeyFieldValues"+ i + "\" VALUE = \"" + this.primaryKeyFieldValues[i] + "\" TYPE = \"HIDDEN\">");
- write("\n<INPUT NAME = \"" + pre + "primaryKeyFieldDataTypes"+ i + "\" VALUE = \"" + this.primaryKeyFieldDataTypes[i] + "\" TYPE = \"HIDDEN\">");
- }
- }
-
- write("\n");
-
- if (this.action != "add") {
- // Decrement global cursor usage counter since this component has tried to use the cursor
- // If the counter has reached zero after doing so, then this is the last cursor using
- // component on the page, so call the DBQuery object's "cursorClose()" method to close
- // the cursor if it's still open.
- decCursorCallCount(this.queryComponent);
- } // end if
-
- } // END utrender
-
-
- //
- // This method outputs all component properties
- //
- function utEmitProperties () {
-
- debug("Writing DB Update Button properties...");
- write("\n<BR><B>MSDBUpdate Properties:</B>");
- write("\n<BR>name = " + this.name);
- write("\n<BR>action = " + this.action);
- write("\n<BR>queryComponent = " + this.queryComponent);
- write("\n<BR>tableName = " + this.tableName);
- write("\n<BR>successURL = " + this.successURL);
- write("\n<BR>errorURL = " + this.errorURL);
- write("\n<BR>primaryKeyFieldCount = " + this.primaryKeyFieldCount);
- var i;
- for (i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
- write("\n<BR>primaryKeyFieldNames" + i + " = " + this.primaryKeyFieldNames[i]);
- write("\n<BR>primaryKeyFieldValues" + i + " = " + this.primaryKeyFieldValues[i]);
- write("\n<BR>primaryKeyFieldDataTypes" + i + " = " + this.primaryKeyFieldDataTypes[i]);
- }
-
- write("\n<BR>fieldCount = " + this.fieldCount);
- for (i = 0; i < parseInt(this.fieldCount,10); i++) {
- write("\n<BR>fieldNames" + i + " = " + this.fieldNames[i]);
- write("\n<BR>fieldValues" + i + " = " + this.fieldValues[i]);
- write("\n<BR>fieldDataTypes" + i + " = " + this.fieldDataTypes[i]);
- }
- write("<BR>");
-
- } // END utEmitProperties
-
-
- //
- // Builds where portion of SQL.
- // SQL Syntax: WHERE PKN1=PKV1 and PKN2=PKV2
- //
- function utBuildWhereSQL () {
-
- var sql;
-
- //debug("Building WHERE portion of SQL");
- if (parseInt(this.primaryKeyFieldCount,10) < 1) debug("0 Primary key fields counted or found");
-
- // SQL Syntax: WHERE PKN1=PKV1 and PKN2=PKV2
- sql = "WHERE";
- for (var fcnt=0; fcnt<parseInt(this.primaryKeyFieldCount,10); fcnt++) {
-
- sql += " " + this.primaryKeyFieldNames[fcnt] + " = " +
- convertForInput(this.primaryKeyFieldDataTypes[fcnt],wrapDelim(this.databaseType, this.primaryKeyFieldDataTypes[fcnt],this.primaryKeyFieldValues[fcnt]));
- if (fcnt < parseInt(this.primaryKeyFieldCount,10)-1) sql += " AND"
- }
-
- return sql;
-
- } // END utBuildWhereSQL
-
-
- //
- // Builds add SQL.
- // SQL Syntax: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
- //
- function utBuildAddSQL () {
-
- var sql;
- var sqlNames = "";
- var sqlValues = "";
-
- //debug("Building add SQL");
- if (this.fieldCount < 0) debug("0 Primary key fields counted or found");
-
- // SQL Syntax: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
- for (var fcnt=0; fcnt<this.fieldCount; fcnt++) {
-
- sqlNames += this.fieldNames[fcnt];
- sqlValues += " " + convertForInput(this.fieldDataTypes[fcnt],wrapDelim(this.databaseType, this.fieldDataTypes[fcnt],this.fieldValues[fcnt]));
- if (fcnt < this.fieldCount-1) {
- sqlNames += ", ";
- sqlValues += ",";
- }
- }
- sql = "INSERT INTO " + this.tableName + " ("+sqlNames+") VALUES ("+sqlValues+")";
-
- return sql;
-
- } // END utBuildAddSQL
-
-
- //
- // Builds delete SQL.
- // SQL Syntax: "DELETE Table WHERE PKN1=PKV1 and PKN2=PKV2
- //
- function utBuildDeleteSQL () {
- var sql;
-
- //debug("Building delete SQL:");
-
- // SQL Syntax: "DELETE Table WHERE PKN1=PKV1 and PKN2=PKV2
- sql = "DELETE FROM " + this.tableName + " " + this.buildWhereSQL();
-
- return sql;
-
- } // END utBuildDeleteSQL
-
-
- //
- // Builds modify SQL.
- // SQL Syntax: "UPDATE TableName SET (FN1=FV1, FN2=FV2, FN3=FV3) WHERE PKN1=PKV1 and PKN2=PKV2
- //
- function utBuildModifySQL () {
- var sql;
- var sqlPairs = "";
-
- //debug("Building modify SQL:");
-
- // SQL Syntax: "UPDATE TableName SET (FN1=FV1, FN2=FV2, FN3=FV3) WHERE PKN1=PKV1 and PKN2=PKV2
- for (var fcnt=0; fcnt<this.fieldCount; fcnt++) {
-
- sqlPairs += this.fieldNames[fcnt] + " = " +
- convertForInput(this.fieldDataTypes[fcnt],wrapDelim(this.databaseType, this.fieldDataTypes[fcnt],this.fieldValues[fcnt]));
- if (fcnt < this.fieldCount-1) {
- sqlPairs += ", ";
- }
- }
- sql = "UPDATE " + this.tableName + " SET "+sqlPairs+" " + this.buildWhereSQL();
-
- return sql;
-
- } // END utBuildModifySQL
-
-
- //
- // Gets database, executes the SQL
- //
- function utExecute () {
-
- var sql = "";
-
- if (Application("globalConn") != "true") {
- write("\r\n<BR>Connecting to database...");
- //debug("Connecting to database...");
- eval(this.dataSource + ".connect()");
- }
- else { // Assume connected
- write("\r\n<BR>Using existing database connection...");
- //debug("Using existing database connection...");
- }
-
- // build and execute: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
- if (Session(this.dataSource) != null) {
- if (this.action=="add") sql = this.buildAddSQL();
- if (this.action=="modify") sql = this.buildModifySQL();
- if (this.action=="delete") sql = this.buildDeleteSQL();
-
- //debug("Executing SQL");
- var resultPage = (ExecuteSQL(this.dataSource,sql)) ? upd1.successURL : upd1.errorURL
- debug("\r\nRedirecting to: " + resultPage);
- if (Application("globalConn") != "true")
- eval(this.dataSource + ".disconnect()");
- Response.Clear();
- Response.Redirect (resultPage);
- Response.End();
-
- }
- else {
- write("\r\n<BR>Unable to open a valid database connection...");
- debug("Unable to open a valid database connection...");
- Response.Clear();
- Response.Redirect (upd1.errorURL);
- Response.End();
- }
-
-
- } // END utExecute
-
-
- //
- // Set parameters for database types after validating them
- //
- function utSetDatabaseTypes(databaseType, ODBCDatabaseType) {
-
- this.databaseType = (databaseType == null) ? "" : databaseType;
- this.ODBCDatabaseType = (ODBCDatabaseType == null) ? "" : ODBCDatabaseType;
-
- } // End utSetDatabaseTypes
-
-
- </SCRIPT>
-
- <SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
-
- function ExecuteSQL (DS,SQL)
- 'Returns successfullness
-
- on error resume next
-
- Session(DS).Errors.Clear()
-
- 'debug("\nExecuting: " + SQL)
- Session(DS).Execute(SQL)
-
- if Session(DS).Errors.Count > 0 then
- Session(DS).Errors.Clear()
- ExecuteSQL = False
- else
- ExecuteSQL = True
- end if
-
- end function
- </SCRIPT>
-
-
-